package top.stmo.stmoBlog.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.stmo.stmoBlog.dao.dos.DiaryDos;
import top.stmo.stmoBlog.dao.mapper.DiaryMapper;
import top.stmo.stmoBlog.dao.vo.ContentVo;
import top.stmo.stmoBlog.dao.vo.DiaryVo;
import top.stmo.stmoBlog.service.DiaryService;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;


@Service
public class DiaryServiceImpl implements DiaryService {


    @Autowired
    private DiaryMapper diaryMapper;


    @Override
    public List<DiaryVo> findDiaryList() {

        List<DiaryDos> diaryList = diaryMapper.findDiaryList();

        List<DiaryVo> diaryVoList = new ArrayList<>();

        Set<String> yearList = new LinkedHashSet<>();

        for(DiaryDos diary : diaryList) {
            String[] diaryArray = diary.getDate().split("-");
            yearList.add(diaryArray[0]+"年");
        }

        for(String year : yearList) {
            DiaryVo diaryVo = new DiaryVo();
            List<ContentVo> contentVoList = new ArrayList<>();
            diaryVo.setContentVoList(contentVoList);
            diaryVo.setYear(year);
            diaryVoList.add(diaryVo);
        }

        for(DiaryDos diary : diaryList) {
            String[] diaryArray = diary.getDate().split("-");
            for(DiaryVo diaryVo : diaryVoList) {
                if(diaryVo.getYear().equals(diaryArray[0]+"年")) {
                    List<ContentVo> contentVoList = diaryVo.getContentVoList();
                    ContentVo contentVo = new ContentVo();
                    contentVo.setContent(diary.getContent());
                    contentVo.setRecordDate(diaryArray[1]+"月"+diaryArray[2]+"日");
                    contentVoList.add(contentVo);
                    break;
                }
            }
        }

        return diaryVoList;
    }
}
